package com.ezops.businessprocess;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.identity.User;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.test.ActivitiRule;
import org.activiti.engine.test.Deployment;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.fasterxml.jackson.core.JsonProcessingException;

//@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration("classpath:activiti.cfg.xml")
public class TaskServiceTestCase {

	//@Rule
	public ActivitiRule activitiRule = new ActivitiRule();
	
	
	
	
	//@Test
	//@Deployment(resources = { "Trade_Exception_Workflow.bpmn" })
	public void testIdentityService() throws JsonProcessingException {
		Map<String, Object> processInstanceVar = new HashMap<String, Object>();
		processInstanceVar.put("nav", 0);
		EzopsModel model = new EzopsModel();
		model.ezOpsId = "2222";
		model.tradeId="1111";
		processInstanceVar.put("message", model);
		ProcessInstance processInstance = activitiRule.getRuntimeService()
				.startProcessInstanceByKey("tradeExceptionProcess",processInstanceVar);
		assertNotNull(processInstance);
		List<User> users = activitiRule.getIdentityService().createUserQuery().list();
		
		assertNotNull(users);
	}

	

	//@Test
	//@Deployment(resources = { "Trade_Exception_Workflow.bpmn" })
	public void test() throws JsonProcessingException {
		Map<String, Object> processInstanceVar = new HashMap<String, Object>();
		processInstanceVar.put("nav", 0);
		EzopsModel model = new EzopsModel();
		model.ezOpsId = "2222";
		model.tradeId="1111";
		processInstanceVar.put("message", model);
		ProcessInstance processInstance = activitiRule.getRuntimeService()
				.startProcessInstanceByKey("tradeExceptionProcess",processInstanceVar);
		assertNotNull(processInstance);
		List<Task> tasks = activitiRule.getTaskService().createTaskQuery()
				.taskDefinitionKey("usertask1").includeProcessVariables().list();

		for (Task task : tasks) {
			
			System.out.println(task.getId());
			System.err.println("process var size"+task.getProcessVariables().size());
//			TaskFormData data =  activitiRule.getFormService().getTaskFormData(task.getId());
//			for(FormProperty property :data.getFormProperties()){
//				System.err.println("Property Name- "+property.getName() + " And property value"+ property.getValue());
//			}

			activitiRule.getTaskService().setVariable(task.getId(), "nav", 10);
			//activitiRule.getTaskService().saveTask(task);
			
		}
		
		Object newNav = activitiRule.getRuntimeService().getVariable(processInstance.getId(), "nav");
		System.err.println("New NAV ####"+newNav);
		assertEquals("Trade Exception Resolution", tasks.get(0).getName());
	}
	
	static class EzopsModel implements Serializable {
		private String ezOpsId;
		public String getEzOpsId() {
			return ezOpsId;
		}
		public void setEzOpsId(String ezOpsId) {
			this.ezOpsId = ezOpsId;
		}
		private String tradeId;
	}

}
